package com.yoo.ons.function.login.viewmodel

import com.framework.network.remote.NetworkObserver
import com.framework.network.remote.endRequest
import com.framework.network.remote.error
import com.framework.network.remote.requestObserver
import com.framework.network.remote.startRequest
import com.framework.network.remote.success
import com.yoo.ons.model.PolicyModel
import com.yoo.ons.base.BaseVMViewModel
import com.yoo.ons.base.HttpRequestListener
import com.yoo.ons.base.Request
import com.yoo.ons.model.LoginResultModel

class LoginViewModel:BaseVMViewModel() {
    /**
     * 获取协议
     */
    fun getPolicy(listener: HttpRequestListener<PolicyModel>){
        request(listener, request = object : Request {
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postPolicy(map)?.requestObserver(
                    NetworkObserver<PolicyModel>().startRequest {
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        requestEndListener?.invoke()
                    }
                )

            }

        })
    }


    fun getLogin(listener: HttpRequestListener<LoginResultModel>){
        request(listener, request = object :Request{
            override fun request(map: HashMap<String, Any>, requestEndListener: (() -> Unit?)?) {
                apiService?.postLogin(map)?.requestObserver(
                    NetworkObserver<LoginResultModel>().startRequest {
                        showLoading()
                        listener.startRequest(it)
                    }.success {
                        listener.success(it)
                    }.error {
                        listener.error(it)
                    }.endRequest {
                        listener.endRequest()
                        hideLoading()
                        requestEndListener?.invoke()
                    }

                )
            }

        })
    }
}